package cn.rongcloud.im.ui.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toolbar;

import cn.rongcloud.im.R;

/**
 * Created by Administrator on 2017/3/21.
 */

public class MoudleWebActivity extends Activity implements View.OnClickListener{
    /**WebView跳转的url*/
    private String url;
    /**标题名字*/
    private String titleName;
    /**模块跳转的WebView*/
    private WebView mMoudleWebView;
    /**回退按钮旁边的文字*/
    private TextView mBackTextView;
    /**回退布局*/
    private LinearLayout mBackLayout;
    /**标题的TextView*/
    private TextView mTitleNameTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_moudle_web);
        initParams();
        initView();
        setView();
        setListener();
    }

    /**
     * 初始化参数
     */
    private void initParams() {
        Bundle bundle = getIntent().getExtras();
        if(null != bundle){
            url = bundle.getString("url");
            titleName = bundle.getString("titleName");
        }
    }

    /**
     * 设置监听
     */
    private void setListener() {
        mBackLayout.setOnClickListener(this);
    }

    /**
     * 设置布局
     */
    private void setView() {
        mBackTextView.setText(getResources().getString(R.string.toolbar_back));
        mTitleNameTextView.setText(titleName);
        setWebView();
    }

    /**
     * 设置WebView
     */
    private void setWebView() {
        if(null == url || url.length() == 0){
            return;
        }
        mMoudleWebView.getSettings().setDomStorageEnabled(true);
        mMoudleWebView.getSettings().setJavaScriptEnabled(true);
        mMoudleWebView.getSettings().setAppCacheEnabled(true);
        mMoudleWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        mMoudleWebView.loadUrl(url);
        mMoudleWebView.setWebViewClient(new WebViewClient(){
            @Override
            public void onLoadResource(WebView view, String url) {
                super.onLoadResource(view, url);
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
            }
        });
    }

    /**
     * 初始化布局
     */
    private void initView() {
        mMoudleWebView = (WebView) findViewById(R.id.wb_moudle);
        mBackTextView = (TextView) findViewById(R.id.tv_back);
        mBackLayout = (LinearLayout) findViewById(R.id.layout_back);
        mTitleNameTextView = (TextView) findViewById(R.id.tv_title_name);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.layout_back:
                finish();
                break;
            default:
                break;
        }
    }
}
